package com.da.lon.wang.xlg.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.da.lon.wang.xlg.MyApplication;
import com.da.lon.wang.xlg.R;
import com.da.lon.wang.xlg.bean.ModifyPhotoBean;
import com.da.lon.wang.xlg.constants.PreferencesConfig;
import com.da.lon.wang.xlg.constants.XLGConstants;
import com.da.lon.wang.xlg.http.HttpLoader;
import com.da.lon.wang.xlg.util.PreferenceUtils;
import com.da.lon.wang.xlg.util.XLGDataUtils;
import com.umeng.analytics.MobclickAgent;

import net.tsz.afinal.http.AjaxParams;

public class UpdateNinkNameActivity extends Activity implements OnClickListener {
    ImageButton left;
    TextView title;
    EditText name;
    Button save;
    ImageButton delete;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.update_nink_name_activity);
        left = (ImageButton) findViewById(R.id.ouka_home_buyer_left);
        title = (TextView) findViewById(R.id.title);
        title.setText("修改昵称");
        name = (EditText) findViewById(R.id.update_nink_name);
        save = (Button) findViewById(R.id.update_nink_save);
        delete = (ImageButton) findViewById(R.id.delete);
        left.setOnClickListener(this);
        delete.setOnClickListener(this);
        save.setOnClickListener(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
        name.setText(PreferenceUtils.getInstance().getSettingStr(PreferencesConfig.USER_NAME, ""));
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ouka_home_buyer_left:
                MyApplication.hideInputWindow(name, UpdateNinkNameActivity.this);
                finish();
                break;
            case R.id.update_nink_save:
                if (name.getText().toString().equals(PreferenceUtils.getInstance().getSettingStr(PreferencesConfig.USER_NAME, ""))) {
//				Toast.makeText(this, "您没有修改您的昵称!", Toast.LENGTH_SHORT).show();
                    finish();
                } else if (name.getText().toString().trim().equals("")) {
                    Toast.makeText(this, "昵称不能为空!", Toast.LENGTH_SHORT).show();
                } else if (name.getText().toString().length() > 20) {
                    Toast.makeText(this, "昵称不能太长!", Toast.LENGTH_SHORT).show();
                } else {
                    update(name.getText().toString());
                    MyApplication.hideInputWindow(name, UpdateNinkNameActivity.this);
                    finish();
                }
                break;
            case R.id.delete:
                name.setText("");
                break;
        }
    }

    /**
     * 更改信息
     *
     * @param text
     */
    private void update(String name) {
        HttpLoader<ModifyPhotoBean> httpLoader = new HttpLoader<ModifyPhotoBean>(this,
                XLGConstants.URL + "User-userinfoedit", ModifyPhotoBean.class);
        AjaxParams params = new AjaxParams();
        params.put("token", XLGDataUtils.encrypt2MD5("User" + "2015#$%" + "userinfoedit"));
        params.put("id", PreferenceUtils.getInstance().getSettingStr(PreferencesConfig.USER_ID, ""));
        params.put("device_type", "1");
        params.put("deviceId", PreferenceUtils.getInstance().getSettingStr(PreferencesConfig.DEVICE_ID, ""));
        params.put("user_key", PreferenceUtils.getInstance().getSettingStr(PreferencesConfig.USER_KEY, ""));
        params.put("sex", PreferenceUtils.getInstance().getSettingStr("sex", ""));
        params.put("birth_year", PreferenceUtils.getInstance().getSettingStr("birth_year", ""));
        params.put("birth_month", PreferenceUtils.getInstance().getSettingStr("birth_month", ""));
        params.put("birth_day", PreferenceUtils.getInstance().getSettingStr("birth_day", ""));
        params.put("nickname", name);
        httpLoader.loadData(params);
        PreferenceUtils.getInstance().SetSettingString(PreferencesConfig.USER_NAME, name);
    }
}
